package com.wd.live.view.activity

import android.Manifest
import android.app.AlertDialog
import android.content.Intent
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.wd.live.R
import com.wd.live.databinding.ActivityMainBinding
import com.wd.live.viewmodel.MainViewModel
import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_list.activity.fragment.ListFragment
import com.wd.mylibrary_live.view.fragment.LiveFragment
import com.wd.mylibrary_my.view.fragment.MyFragment
import com.wd.mylibrary_near.view.fragment.NearFragment
import com.wd.mylibrary_square.view.fragment.SquareFragment
import com.wd.mylibrary_live.R.layout.alertdialog


/**
 * 作者: 邢凌志
 * 时间: 2024/9/12
 * 描述: 主页面框架
 */

@Route(path = "/main/MainActivity")
class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(R.layout.activity_main) {

    var userId=MyApp.sp.getInt("userId",0)
    var sessionId=MyApp.sp.getString("sessionId","0").toString()

    var title= arrayOf("广场","附近","","榜单","我的")
    var fragmentList= arrayOf(SquareFragment(), NearFragment(),
        LiveFragment(),ListFragment(), MyFragment()
    )
    var icon1= arrayOf(R.mipmap.square,R.mipmap.position,R.mipmap.dicolor,R.mipmap.list,R.mipmap.my)
    var icon2= arrayOf(R.mipmap.square_select,R.mipmap.position_select,R.mipmap.dicolor,R.mipmap.list_selected,R.mipmap.my_select)

    override fun initView() {

        //开启权限
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            requestPermissions(arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE
            ),1)
        }

        for (s in title) {
            dataBinding.tab.addTab(dataBinding.tab.newTab().setText(s))
        }

        dataBinding.vp.adapter=object :FragmentPagerAdapter(supportFragmentManager){
            override fun getCount(): Int {
                return title.size
            }

            override fun getItem(position: Int): Fragment {
                return fragmentList.get(position)
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return title[position]
            }
        }

        dataBinding.tab.setupWithViewPager(dataBinding.vp)

        dataBinding.vp.offscreenPageLimit=fragmentList.size-1

        dataBinding.tab.getTabAt(0)?.setIcon(R.mipmap.square_select)
        dataBinding.tab.getTabAt(1)?.setIcon(R.mipmap.position)
        dataBinding.tab.getTabAt(2)?.setIcon(R.mipmap.dicolor)
        dataBinding.tab.getTabAt(3)?.setIcon(R.mipmap.list)
        dataBinding.tab.getTabAt(4)?.setIcon(R.mipmap.my)

        dataBinding.tab.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.setIcon(icon2[tab.position]!!)

                when(tab?.position){
                    2->{
                        viewModel.getCheckData(userId,sessionId)
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.setIcon(icon1[tab.position]!!)
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

    }

    override fun dataObserve() {
        viewModel.checkData.observe(this,{

            if(it.result!=null){
                if(it.result.status){
                    Toast.makeText(this, "已完成实名认证", Toast.LENGTH_SHORT).show()
                }else{
                    var builder = AlertDialog.Builder(this@MainActivity)
                    val inflater = LayoutInflater.from(this@MainActivity)
                    val view = inflater.inflate(alertdialog, null)

                    val messageTextView = view.findViewById<TextView>(com.wd.mylibrary_live.R.id.messageTextView)
                    val cancelButton = view.findViewById<Button>(com.wd.mylibrary_live.R.id.cancelButton)
                    val okButton = view.findViewById<Button>(com.wd.mylibrary_live.R.id.okButton)

                    messageTextView.text = "该功能需要进行实名认证！"

                    cancelButton.setOnClickListener {
                        startActivity(Intent(this@MainActivity,MainActivity::class.java))
                        finish()
                    }

                    okButton.setOnClickListener {
                        startActivity(Intent(this@MainActivity,MainActivity::class.java))
                    }

                    builder.setView(view)
                    builder.create()
                    builder.show()
                }
            }

        })
        Log.i("onDestroy", "onDestroy: 清空myApp")
        MyApp.sp.edit().clear()
    }



}